package com.lemon.springboot.service.impl;

import com.lemon.springboot.dao.IDiskDao;
import com.lemon.springboot.entity.CpuVo;
import com.lemon.springboot.entity.DiskVo;
import com.lemon.springboot.service.IDiskPageService;
import com.lemon.springboot.utils.DateUtil;
import com.lemon.springboot.utils.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author liuli
 * @Date 2020/12/21 11:10
 */
@Service
public class DiskPageServiceImpl implements IDiskPageService {

    @Autowired
    IDiskDao diskDao;

    @Override
    public Map<String, Object> findDiskDataByDate(Date startTime, Date endTime, String token, int maxDataCount) {
        Map<String, Object> result =  new HashMap<>();
        List<DiskVo> diskDataList = diskDao.findDiskByDate(startTime, endTime, token);
        List<String> field = Arrays.asList(new String[maxDataCount]);
        Map<String, List<String>> dataMap = null;
        Map<String, String> modelMap = null;
        for (int i = 0; i < maxDataCount; i = i+2) {
            if (i == 0) {
                field.set(i, DateUtil.dateFormat(new Date(startTime.getTime() + i * DateUtil._30SEC), DateUtil.parent));
            }else {
                field.set(i, DateUtil.dateFormat(new Date(startTime.getTime() + i * DateUtil._30SEC), "HH:mm"));
            }
            result.put("fields", field);
        }
        if (diskDataList != null && diskDataList.size() > 0) {
            dataMap = new HashMap<>();
            modelMap = new HashMap<>();
            for (DiskVo diskVo : diskDataList) {
                if (!modelMap.containsValue(diskVo.getDevName())) {
                    modelMap.put(diskVo.getDevName(), diskVo.getDevName() + "总空间:" + NumberUtil.floatFormat(((double) diskVo.getDiskTotal()) / 1024 / 1024, "0.0") + "GB");
                }
                List<String> data = dataMap.computeIfAbsent(diskVo.getDevName(), k -> Arrays.asList(new String[maxDataCount]));
                long time = diskVo.getDate().getTime();
                int index = ((int) ((time - startTime.getTime()) / DateUtil._30SEC));
                data.set(index, NumberUtil.floatFormat(((double) diskVo.getDiskUse()) / 1024 / 1024, "0.0"));
            }
            result.put("data", dataMap);
            result.put("model", modelMap);
        }
        return result;
    }
}
